package com.taurus.myboard.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taurus.myboard.common.vo.Result;
import com.taurus.myboard.entity.User;
import com.taurus.myboard.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.repository.query.Param;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kanten
 * @since 2023-06-07
 */
@Api(tags = "用户接口列表")
@RestController
@RequestMapping("/user")
//@CrossOrigin
public class UserController {
    @Resource
    private IUserService userService;

    @Resource
    private PasswordEncoder passwordEncoder;

    @GetMapping("/all")
    public Result<List<User>> getAllUsers(){
        List<User> list = userService.list();
        return Result.success(list);
    }

    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody User user){
        Map<String, Object> data = userService.login(user);
        if (null == data)return Result.fail(20001, "用户名或密码错误");
        return Result.success(data);
    }

    @GetMapping("/info")
    public Result<?> getUserInfo(@Param("token") String token){
        // 根据token获取用户信息，redis
        Map<String,Object> data = userService.getUserInfo(token);
        if(data != null){
            return Result.success(data);
        }
        return Result.fail(20003,"用户信息获取失败");
    }

    @PostMapping("/logout")
    public Result<?> logout(@RequestHeader("X-Token") String token){
        userService.logout(token);
        return Result.success("注销成功");
    }

    @GetMapping("/list")
    public Result<Map<String, Object>> getUserList(@RequestParam(value = "username", required = false) String username,
                                                 @RequestParam(value = "phone", required = false) String phone,
                                                 @RequestParam(value = "pageNo") Long pageNo,
                                                 @RequestParam(value = "pageSize") Long pageSize){
        // 条件判断
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(username), User::getUsername, username);
        wrapper.eq(StringUtils.hasLength(phone), User::getPhone, phone);
        wrapper.orderByDesc(User::getId);
        // 分页
        Page<User> page = new Page(pageNo, pageSize);
        userService.page(page, wrapper);

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("total", page.getTotal());
        data.put("rows", page.getRecords());
        return Result.success(data);
    }

    @PostMapping("/add")
    public Result<?> addUser(@RequestBody User user){
        userService.addUser(user);
        return Result.success("新增用户成功");
    }

    @PutMapping("/update")
    public Result<?> updateUser(@RequestBody User user){
        userService.updateUser(user);
        return Result.success("修改用户成功");
    }

    @GetMapping("{id}")
    public Result<User> getUserById(@PathVariable("id") Integer id){
        User user = userService.getUserById(id);
        return Result.success(user);
    }

    @DeleteMapping("{id}")
    public Result<User> delUserById(@PathVariable("id") Integer id){
        try{
            userService.delUserById(id);
            return Result.success("删除用户成功");
        }catch(EmptyResultDataAccessException e){
            return Result.fail("删除用户失败");
        }
    }
}
